/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package testklient;

import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.net.Socket;
import java.util.logging.Level;
import java.util.logging.Logger;
import theserver.DeviceInfo;
import theserver.DeviceList;
import theserver.VenueActivity;
import theserver.VenueList;

/**
 *
 * @author Emma
 */
public class Socketocketsaken implements Runnable{

    private Socket socket;
    private Thread t = new Thread(this);
    private ObjectInputStream objectInputStream;
    private Object inputStreamObject;
    private DeviceInfo mySelf;
    private GUI gui;
    public Socketocketsaken(Socket s, DeviceInfo mySelf, GUI gui) {
        this.gui = gui;
        this.mySelf = mySelf;
        socket = s;
        t.start();
    }

    public void run() {
        try {
            objectInputStream = new ObjectInputStream(new BufferedInputStream(socket.getInputStream()));
            while (true) {
                try {
                    while ((inputStreamObject = objectInputStream.readObject()) != null) {
                        System.err.println("Tråden i socketsocksaken innan objektkoll");
                        if(inputStreamObject instanceof DeviceList){
                        DeviceList dev = (DeviceList) inputStreamObject;
                            for (int i = 0; i < dev.size(); i++) {
                                if(dev.get(i).getDeviceID().equals(mySelf.getDeviceID())){
                                dev.remove(i);
                                }
                               
                            }
                        gui.addToJList1(dev);
                        }
                        if(inputStreamObject instanceof VenueList){
                        VenueList ven   = (VenueList) inputStreamObject;
                        gui.setVenue(ven);

                        }

                    }
                } catch (ClassNotFoundException ex) {
                    Logger.getLogger(Socketocketsaken.class.getName()).log(Level.SEVERE, null, ex);
                }

            }
        } catch (IOException ex) {
            Logger.getLogger(Socketocketsaken.class.getName()).log(Level.SEVERE, null, ex);
        }
    }


}
